package com.hk.core.autoconfigure.exception.servlet;

import com.hk.commons.JsonResult;
import com.hk.commons.util.SpringContextHolder;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 数据库访问 Exception
 *
 * @author kevin
 * @date 2018-08-03 10:00
 */
@RestControllerAdvice
public class DataExceptionHandler extends AbstractExceptionHandler {

    /**
     * 对数据库操作出现的所有异常： DataAccessException
     *
     * @param e DataAccessException
     * @return {@link JsonResult}
     */
    @ExceptionHandler(value = {DataAccessException.class})
    @ResponseStatus(HttpStatus.OK)
    public JsonResult<Void> dataAccessException(DataAccessException e, HttpServletRequest request) {
        error(e, e.getMessage(), request);
        return JsonResult.error(SpringContextHolder.getMessage("server.error.message"));
    }
}
